package com.fanghaoran.notes.controller;

import com.alibaba.fastjson.JSON;
import com.fanghaoran.notes.entity.User;
import com.fanghaoran.notes.service.EvaluationService;
import com.fanghaoran.notes.service.impl.EvaluationServiceImpl;
import com.fanghaoran.notes.util.Constants;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

/**
 * @Author: win11
 * @Date: 2023/04/24/11:46
 * @Description: 评价业务
 */
@WebServlet("/evaluation")
public class EvaluationController extends BaseServlet{

    /**
     * service对象
     */
    EvaluationService evaluationService=new EvaluationServiceImpl();

    /**
     * 展示评价
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void showEvaluation(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取前端传来的iid(订单项)参数
        String iid = request.getParameter("iid");
        //业务层处理(找对应评价表数据) 并返回结果
        response.getWriter().println(JSON.toJSONString(evaluationService.showEvaluation(Integer.parseInt(iid))));
    }

    /**
     * 添加评论
     * @param request 请求
     * @param response 响应
     * @throws Exception 异常
     */
    public void addEvaluation(HttpServletRequest request,HttpServletResponse response) throws Exception {
        //接收前端参数
        int pid = Integer.parseInt(request.getParameter("pid"));
        int iid = Integer.parseInt(request.getParameter("iid"));
        int score = Integer.parseInt(request.getParameter("score"));
        String comment = request.getParameter("comment");
        //获取用户
        User user=(User)request.getSession().getAttribute(Constants.LOGIN_SESSION_PARAM);
        Integer uid = user.getUId();
        //业务层存储数据
        response.getWriter().println(JSON.toJSONString(evaluationService.addEvaluation(uid,iid,score,pid,comment)));
    }
}
